import DataSelect from '@/components/DataSelect';
import TauSimpleSelect from '@/components/TauSimpleSelect';
import { ellipsisRender } from '@/components/TauTable';
import CitySelect from '@/components/biz/CitySelect';
import RegionGroupSelect from '@/components/biz/RegionGroupSelect';
import * as BASE from '@/stores/definitions-base';
import { Input, Switch } from '@alifd/next';
import { t } from '@lingui/macro';
import React from 'react';

const {
  DATETIME_TEXT,
  DATE_TEXT,
  TIME_TEXT,
  EMPTY_TEXT,
  ENUMS: ENUMSBase,
  FieldsGrid: FieldGridBase,
  TitleGrid,
  column: columnBase,
  toSelectDataSource,
} = BASE;
export { DATETIME_TEXT, DATE_TEXT, EMPTY_TEXT, TIME_TEXT, TitleGrid, toSelectDataSource };

/**
 * @param {*} path        属性path
 * @param {*} props       column属性
 * @returns column定义
 */
export function column(...args) {
  return columnBase(model, ...args);
}

/**
 * 显示一组表单字段
 * ```js
 * {
 *   fields: string[],    // 字段名称，可以使用.来指定嵌套对象属性，例如order.busOrder.shift
 *   columns: number,     // 列数，默认为4,最大为12,最小为1
 *   model: object,       // 字段定义数据模型的根对象，默认不指定，仅在使用子对象作为根模型时设置
 *   overrideModel: object, // 重载字段定义数据模型
 *   mode: string,        // 表单模式，默认为edit，可选view
 *   itemLayout: object,  // FormItem布局配置，默认为{}
 * }
 * ```
 *
 * @param {object} param0
 * @returns
 */
export function FieldsGrid(props) {
  return <FieldGridBase model={model} {...props} />;
}

//----------------------------------------------------------------------
export const ENUMS = {
  ...ENUMSBase,
  rangeType: {
    1: t`同城`,
    2: t`跨境`,
  },
};

/**
 * 模型定义：
 * 1. 普通文本仅需要定义label
 * 2. 需要通过枚举转换的值，定义text; 当使用renderer返回组件时，text属性无效，需要通过组件的renderPreview来指定预览时的绘制方式
 * 3. 通用的ui属性需要定制时，定义ui; 特定模式(view, edit, add)下的ui，添加后缀定义为ui_<mode>，例如：ui_view, ui_edit, ui_add, ui_table
 * 4. ui可配置属性包括:
 *    renderer: 内容渲染函数, Form表单中的原型为：(value: any, formData: any, fieldDefinition: any) => ReactNode; Table Column中的原型为: (value: any, columnIndex: number, rowData: any) => ReactNode;
 *    itemProps: 表单Form.Item的属性，可以是对象，或函数({ isPreview: boolean, mode: string, value: any, formData: any, ...otherDefinitions}) => void;
 *    span: Grid Cell占用的列数, 默认为1
 *
 * 【注】获取接口的model定义后，vscode中通过正则替换：^(\s+)"label": "([^']+)"  ===> $1label: t`$2`
 */
export const model = {
  startMinorAreasGroup: { // 前端添加字段
    label: t`始发区域`,
    ui: {
      renderer: (v, formData) => <RegionGroupSelect useDetailValue cityId={formData?.startMainArea} value={v} />,
      itemProps: { required: true },
    },
  },
  endMinorAreasGroup: { // 前端添加字段
    label: t`到达区域`,
    ui: {
      renderer: (v, formData) => <RegionGroupSelect useDetailValue cityId={formData?.endMainArea} value={v} />,
      itemProps: { required: true },
    },
  },

  portTypeNames: {
    label: '',
  },
  shiftTime: {
    label: t`传递的当天班次时间`,
  },
  startMainAreaName: {
    label: t`开始城市名称`,
  },
  endMainAreaName: {
    label: t`结束城市名称`,
  },
  startMinorAreaNames: {
    label: t`开始区域名称`,
  },
  endMinorAreaNames: {
    label: t`结束区域名称`,
  },
  fromGroupNames: {
    label: t`开始分组名称`,
  },
  toGroupNames: {
    label: t`结束分组名称`,
  },
  returnShift: [
    {
      label: t`回程班次时刻列表`,
    },
  ],
  id: {
    label: '',
  },
  state: {
    label: t`启用`,
    ui_table: {
      renderer: (v, _, row) => <Switch checkedChildren="启用" unCheckedChildren="禁用" checked={Number(v) === 1} size="small" autoWidth onClick={() => rootStore.ccPriceStore.updateState(row)} />,
    },
  },
  portTypes: [
    {
      label: t`口岸类型，0 - 深圳湾口岸；1 - 澳门口岸；2 - 珠海口岸`,
    },
  ],
  portType: {
    label: t`备用字段`,
  },
  companyDiscounts: [
    {
      label: t`承运公司`,
      companyId: {
        label: t`公司id`,
      },
      companyName: {
        label: t`公司名称`,
      },
      settleDiscount: {
        label: t`折扣比例`,
      },
      weight: {
        label: t`权重`,
      },
      hourlyRate: {
        label: t`时租单价`,
      },
      specificSingleFare: {
        label: t`点对点单程价`,
      },
      specificReturnFares: {
        label: t`点对点往返价`,
      },
    },
  ],
  companyId: {
    label: t`公司id`,
  },
  beginName: {
    label: t`开始地址`,
  },
  beginTude: {
    label: t`开始地址经纬度`,
  },
  endName: {
    label: t`结束地址`,
  },
  endTude: {
    label: t`结束地址经纬度`,
  },
  startMainArea: {
    label: t`始发城市`,
    ui: {
      renderer: (v) => <CitySelect value={v} />,
      itemProps: { required: true },
    },
  },
  startMinorAreas: {
    label: t`始发区域`,
  },
  startMinorArea: {
    label: t`开始区域编号`,
  },
  endMainArea: {
    label: t`到达城市`,
    ui: {
      renderer: (v) => <CitySelect value={v} />,
      itemProps: { required: true },
    },
  },
  endMinorAreas: {
    label: t`到达区域`,
  },
  endMinorArea: {
    label: t`结束区域编号`,
  },
  priceType: {
    label: t`类型：1-包车 2-专线`,
  },
  type: {
    label: t`线路类型`, // t`1-同城 2-跨境`,
    text: (v) => ENUMS.rangeType[v],
    ui: {
      renderer: (v) => <TauSimpleSelect data={toSelectDataSource(ENUMS.rangeType)} value={v} hasClear={false} />,
    },
  },
  remarks: {
    label: t`备注`,
    ui: {
      renderer: (v) => <Input.TextArea value={v} />,
      span: 3,
    },
  },
  isNull: {
    label: t`是否为空`,
  },
  isReturn: {
    label: t`是否需要创建返程线路，true：去程线路，false：返程线路`,
  },
  returnId: {
    label: t`往返关联id`,
  },
  cprice: {
    label: t`点对点与包车价格`,
    oneTripPrice: {
      RMB: {
        label: t`单程价格`,
        text: (v) => `¥${v || 0}`,
        ui: {
          renderer: (v) => <Input value={v} addonTextBefore="RMB" innerAfter={t`元`} />,
          itemProps: { required: true, format: 'number', max: 10000 },
        },
      },
    },
    roundTripPrice: {
      RMB: {
        label: t`往返价格`,
        text: (v) => `¥${v || 0}`,
        ui: {
          renderer: (v) => <Input value={v} addonTextBefore="RMB" innerAfter={t`元`} />,
          itemProps: { format: 'number', max: 10000 },
        },
      },
    },
    predictTime: {
      label: t`预估时长`,
      ui: {
        renderer: (v) => <Input value={v} innerAfter={t`小时`} />,
        itemProps: { required: true, format: 'number', max: 720 },
      },
    },
    mileage: {
      label: t`预估里程`,
      ui: {
        renderer: (v) => <Input value={v} innerAfter={t`公里`} />,
        itemProps: { required: true, format: 'number', max: 1000 },
      },
    },
  },
  hprice: {
    label: t`小时租车价格`,
    oneTripPrice: {
      RMB: {
        label: t`小时单价`,
        text: (v) => `¥${v || 0}`,
        ui: {
          renderer: (v) => <Input value={v} addonTextBefore="RMB" innerAfter={t`元`} />,
          itemProps: { required: true, format: 'number', min: 1, max: 10000 },
        },
      },
    },
    roundTripPrice: {
      label: t`往返票种map`,
    },
    minHours: {
      label: t`最低时长`,
      ui: {
        itemProps: { required: true, format: 'number', min: 1, max: 720 },
      },
    },
    mileage: {
      label: t`预估里程`,
    },
  },
  endMainAreaDisable: {
    label: t`选择线路时判断结束城市是否禁用`,
  },
  sortWeight: {
    label: t`排序权重`,
  },
  crBy: {
    label: t`创建者`,
  },
  upBy: {
    label: t`最后修改者`,
  },
  crDate: {
    label: t`创建时间`,
  },
  upDate: {
    label: t`最后修收时间`,
  },
  tenantId: {
    label: t`租户ID`,
  },
  beginAreaName: {
    label: t`始发区域`,
    text: (v, price) => <>{price.type === '1' ? null : <><span style={{ fontWeight: 500 }}>{price.fromGroupNames || '(无分组)'}</span><br /></>}<span style={{ color: 'grey' }}>{ellipsisRender(`${`${price.startMainAreaName} - ${price.startMinorAreaNames}`}`)}</span></>,
  },
  endAreaName: {
    label: t`到达区域`,
    text: (v, price) => <>{price.type === '1' ? null : <><span style={{ fontWeight: 500 }}>{price.toGroupNames || '(无分组)'}</span><br /></>}<span style={{ color: 'grey' }}>{ellipsisRender(`${price.endMainAreaName} - ${price.endMinorAreaNames}`)}</span></>,
  },
};
